/**
 * 
 */
package com.oauth.auth.controllers;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.oauth.auth.constants.AuthConstants;

/**
 * @author asanthan
 *
 */

public class LogOutController implements org.springframework.web.servlet.mvc.Controller{

	@Value("logout")
	private String mViewName;
	@Value("/auth/login")
	private String mLoginUrl;
	
	
	public ModelAndView handleRequest(HttpServletRequest pRequest,HttpServletResponse pResponse) 
					throws ServletException, IOException {
		ModelAndView modelAndView = new ModelAndView();
		
		UserService userService = UserServiceFactory.getUserService();
		
		if(userService == null || userService.getCurrentUser() == null){
	       	pResponse.sendRedirect(getLoginUrl());
	       	return modelAndView;
	    }
	        
		   
        String googleLogOutUrl = userService.createLogoutURL(getLoginUrl());
		modelAndView.addObject(AuthConstants.LOGOUT_URL,googleLogOutUrl);
		modelAndView.setViewName(getViewName());
		
		return modelAndView;
	}


	/**
	 * @return the viewName
	 */
	public String getViewName() {
		return mViewName;
	}


	/**
	 * @param pViewName the viewName to set
	 */
	public void setViewName(String pViewName) {
		mViewName = pViewName;
	}


	/**
	 * @return the loginUrl
	 */
	public String getLoginUrl() {
		return mLoginUrl;
	}


	/**
	 * @param pLoginUrl the loginUrl to set
	 */
	public void setLoginUrl(String pLoginUrl) {
		mLoginUrl = pLoginUrl;
	}


	
}